﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using System.Data;

namespace WaiGaoQiaoWeb.Controllers
{
    public class AccessPermissionsDistributionController : Controller
    {
        private AccessPermissionsDistributionInfo funinfo = new AccessPermissionsDistributionInfo();
        public ActionResult AccessPermissionsDistributionIndex()
        {
            ViewData["deptName"] = GetdeptName();
            ViewData["Machine"] = GetMachine();
            return View();
        }

        /// <summary>
        /// 获取部门列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetdeptName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = funinfo.GetdeptName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["Dep_ID"]), ds.Tables[0].Rows[i]["deptName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetGroupDeptChange(string depid)
        {
            ViewData["GroupDept"] = GetGroupDept(depid);
            return Json(ViewData["GroupDept"]);
        }

        /// <summary>
        /// 获取班组列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetGroupDept(string depid)
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = DeptDDLInfo.GetGroupDept(depid);
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["groupid"]), ds.Tables[0].Rows[i]["groupname"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 获取门名称列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetMachine()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = funinfo.GetMachine();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["MACID"]), ds.Tables[0].Rows[i]["设备描述"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public ActionResult GetList(PageVM pvm)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(pvm.query))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = pvm.query.Split(',');
                if (param[0] != "")
                {
                    query += " and EMPNO='" + param[0].ToString() + "' ";
                }
                if (param[1] != "")
                {
                    query += " and 姓名 like'%" + param[1].ToString() + "%' ";
                }
                if (param[2] != "-1")
                {
                    query += " and DEP_ID='" + param[2].ToString() + "' ";
                }
                if (param[3] != "")
                {
                    query += " and CARDSERIAL='" + param[3].ToString() + "' ";
                }
                if (param[4] != "-1")
                {
                    query += " and MACID='" + param[4].ToString() + "' ";
                }
            }
            IList<AccessPermissionsDistributionInfo> funlist = funinfo.GetList(pvm.rp, pvm.page, query);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = funinfo.GetCount(query);
            List<string> v;
            foreach (AccessPermissionsDistributionInfo item in funlist)
            {
                #region
                v = new List<string>();
                v.Add(item.rownumber.ToString());
                v.Add(item.deptName);
                v.Add(item.PName);
                v.Add(item.EmpNo);
                v.Add(item.NodeName);
                v.Add(item.CardSerial);
                v.Add(item.RECORDDATETIME.ToString());
                #endregion
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = item.rownumber.ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 点击分配权限时执行的方法
        /// </summary>
        /// <returns></returns>
        public ActionResult AssignPermissions()
        {
            ViewData["deptName"] = GetdeptName();
            ViewData["cardleft"] = SelLeftBox("");
            ViewData["cardright"] = SelRightBox();
            ViewData["allmodular"] = GetModularForCheckBox();
            ViewData["rightboxReturn"] = SelRightBoxReturn();
            ViewData["GroupDept"] = GetGroupDept("-1");
            ViewData["StructrueName"] = GetStructrueName();
            return View();
        }

        /// <summary>
        /// 获取岗位列表（用于绑定下拉框）
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> GetStructrueName()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            DataSet ds = funinfo.GetStructrueName();
            dic.Add(-1, "全部");
            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                dic.Add(Convert.ToInt32(ds.Tables[0].Rows[i]["structrueId"]), ds.Tables[0].Rows[i]["structrueName"].ToString().Trim());
            }

            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 点击删除权限时执行的方法
        /// </summary>
        /// <returns></returns>
        public ActionResult DeletePermission()
        {
            ViewData["deptName"] = GetdeptName();
            ViewData["cardleft"] = SelLeftBox("");
            ViewData["cardright"] = SelRightBox();
            ViewData["allmodular"] = GetModularForCheckBox();
            ViewData["rightboxReturn"] = SelRightBoxReturn();
            ViewData["GroupDept"] = GetGroupDept("-1");
            ViewData["StructrueName"] = GetStructrueName();
            return View();
        }

        /// <summary>
        /// 获取所有咨询门禁
        /// </summary>
        /// <returns></returns>
        public IList<AccessPermissionsDistributionInfo> GetModularForCheckBox()
        {
            IList<AccessPermissionsDistributionInfo> mlist = funinfo.GetModularAll();
            return mlist;
        }

        public ActionResult GetLeftBox(string Select)
        {
            ViewData["cardleft"] = SelLeftBox(Select);
            return Json(ViewData["cardleft"]);
        }

        /// <summary>
        /// 获取下拉列表
        /// </summary>
        /// <returns></returns>
        public IList<SelectListItem> SelLeftBox(string Select)
        {
            string query = string.Empty;
            if (string.IsNullOrEmpty(Select))
            {
                query = " and 1=1";
            }
            else
            {
                string[] param = Select.Split(',');
                string strDep_ID = "";
                string strCardSerial = " and 1=1 ";
                string strGroupIdDept = " and 1=1 ";
                string strstructrueId = " and 1=1 ";
                if (param[2] != "-1")
                {
                    strDep_ID = " and Dep_ID='" + param[2] + "'";
                }
                else
                {
                    strDep_ID += " and 1=1 ";
                }
                if (param[3] != "")
                {
                    strCardSerial = " and CardSerial=" + param[3];
                }
                if (param[4] != "-1")
                {
                    strGroupIdDept = " and GroupIdDept=" + param[4];
                }
                if (param[5] != "-1")
                {
                    strstructrueId = " and 岗位编号=" + param[5];
                }
                query = " and EmpNo like '%" + param[0] + "%' and PName like '%" + param[1] + "%'" + strDep_ID + strCardSerial + strGroupIdDept + strstructrueId;
            }
            IList<AccessPermissionsDistributionInfo> funlist = funinfo.SelLeftBox(query);
            IDictionary<string, string> dic = new Dictionary<string, string>();
            foreach (AccessPermissionsDistributionInfo item in funlist)
            {
                dic.Add(item.EmpNo, item.PName);
            }
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public IList<SelectListItem> SelRightBox()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        public IList<SelectListItem> SelRightBoxReturn()
        {
            IDictionary<int, string> dic = new Dictionary<int, string>();
            IList<SelectListItem> selectList =
                dic.Select(m => new SelectListItem() { Text = m.Value.ToString(), Value = m.Key.ToString() }).ToList();
            return selectList;
        }

        /// <summary>
        /// 新增门禁权限
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="PID"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public ActionResult BindMenuToRole(string mid, string EMPNO, string Name)
        {
            string strReturn = funinfo.BindMenuToRole(mid, EMPNO, Name);
            return Json(strReturn);
        }

        /// <summary>
        /// 删除门禁权限
        /// </summary>
        /// <param name="mid"></param>
        /// <param name="PID"></param>
        /// <param name="Name"></param>
        /// <returns></returns>
        public ActionResult BindMenuToRoleEdit(string mid, string EMPNO, string Name)
        {
            string strReturn = funinfo.BindMenuToRoleEdit(mid, EMPNO, Name);
            return Json(strReturn);
        }
    }
}
